﻿using B.S.Traceable.Domain.Entitiy;
using B.S.Traceable.ErrorCode;
using B.S.Traceable.Infrasturcture.Interfaces;
using B.S.Traceable.Write.Api.Application.Command.Traceable;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace B.S.Traceable.Write.Api.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TraceableController : ControllerBase
    {
        private readonly IMediator mediator;

        public TraceableController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        /// <summary>
        /// 添加商品信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<object>> CreateTraceable(CreateTraceableCommand request)
        {
            return await mediator.Send(request);
        }

        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> UploadIamge(IFormFile file)
        {
            var res = "wwwroot/tu/" + file.FileName;
            using (FileStream fs=new FileStream(res,FileMode.OpenOrCreate))
            {
                file.CopyTo(fs);
            }
            return Ok("https://localhost:7090/tu/"+file.FileName);
        }

        /// <summary>
        /// 删除商品信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut]
        public Task<ApiResult<object>> DelTraceable([FromQuery]DelTraceableCommand request)
        {
            return mediator.Send(request);
        }
        /// <summary>
        /// 修改商品信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut]
        public Task<ApiResult<object>> UpdateTraceable(UpdateTraceableCommand request)
        {
            return mediator.Send(request);
        }







    }
}
